package com.jinlejia.erp.ui.mine.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ItemActualSellingBinding;
import com.wang.avi.AVLoadingIndicatorView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import androidx.databinding.DataBindingUtil;

/**
 * 我的钥匙买卖（adapter）
 */

public class MyKeyAdapter extends YfListAdapter<Map<String, Object>> {
    private Context context;
    private OnNumberListener listener;

    public MyKeyAdapter(ArrayList<Map<String, Object>> data) {
        super(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        context = parent.getContext().getApplicationContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_actual_selling, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);
        TextView txt = view.findViewById(R.id.id_tips);
        txt.setText("暂无数据");
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        ItemActualSellingBinding binding = DataBindingUtil.getBinding(viewHolder.itemView);
        final Map<String, Object> map = mData.get(position);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        if (map.containsKey("rscode")) {//房源编号
            binding.houseNumber.setText(map.get("rscode").toString());
            binding.houseNumber.setTextColor(context.getResources().getColor(R.color.blue));
        }
        if (map.containsKey("createTime"))  //提交时间
            binding.sellingTime.setText(sdf.format(new Date(Long.valueOf(map.get("createTime").toString()))));

        if (map.containsKey("estate"))  //小区名称
            binding.communityName.setText(map.get("estate").toString());

        if (map.containsKey("state")){//审核状态
            binding.sellingState.setTextColor(context.getResources().getColor(R.color.red));
            switch (map.get("state").toString()){
                case "0":
                    binding.sellingState.setText("待审核");
                    break;
                case "1":
                    binding.sellingState.setText("审核通过");
                    binding.sellingState.setTextColor(context.getResources().getColor(R.color.green));
                    break;
                case "2":
                    binding.sellingState.setText("驳回");
                    break;
            }
        }
        binding.houseNumber.setOnClickListener(view -> listener.OnNumber(map.get("rscode").toString()));
        binding.getRoot().setTag(mData.get(position));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new FooterViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateErrorViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.error_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindFooterViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindFooterViewHolder(holder, position);
        if (mFooters.get(0).equals(R.string.is_all)) {
            ((FooterViewHolder) holder).txt.setText(R.string.is_all);
            ((FooterViewHolder) holder).txt.setVisibility(View.VISIBLE);
            ((FooterViewHolder) holder).progress.setVisibility(View.GONE);
        } else {
            ((FooterViewHolder) holder).txt.setVisibility(View.GONE);
            ((FooterViewHolder) holder).progress.setVisibility(View.VISIBLE);
        }
    }

    static final class FooterViewHolder extends RecyclerView.ViewHolder {
        TextView txt;
        AVLoadingIndicatorView progress;

        public FooterViewHolder(View itemView) {
            super(itemView);
            txt = itemView.findViewById(R.id.txt);
            progress = itemView.findViewById(R.id.progress);
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder{

        ViewHolder(View view) {
            super(view);
            DataBindingUtil.bind(view);
        }
    }
    public interface OnNumberListener{
        void OnNumber(String rscode);
    }

    public void setOnNumberListener(OnNumberListener listener){
        this.listener = listener;
    }
}
